package com.budwk.app.sqfwV2.generator.dayingRoadExcel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.dto.dayingRoad.DaYingLuRedRecordPdfDTO;
import com.budwk.app.sqfwV2.dto.image.DaYingLuRedRecordExcelImageConfigBO;
import com.budwk.app.sqfwV2.enums.RedWhite;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * excel云岩区红事跟踪报备月台账表.xlsx
 */
public class RedTrackingReportExcelGenerator extends BaseTemplateExcelGenerator {

    @Override
    protected String getTemplateName() {
        return "daYingLu/red/trackingReport.xlsx";
    }

    /**
     * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
     *
     * @return 文件名名称
     */
    @Override
    public String getDownloadFileName() {
        //TODO 在这里返回你生成的文件名名称
        // 名称要求带上随机数。如果仅仅是一个中文名称的话，上传到对象存储会造成文件覆盖
        // 注意注意注意不用带上后缀
        return "云岩区红事跟踪报备月台账表";
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     *
     * @param excelParam       已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        // 是否只生成模板
        Boolean onlyGenerateTemplateFlag = excelCreateParam.getExcelCreateConfig().getOnlyGenerateTemplateFlag();
        if (onlyGenerateTemplateFlag) {
            for (String key : excelParam.keySet()) {
                excelParam.put(key, "");
            }
            return;
        }

        String district = excelCreateParam.getBaseExcelParamDTO().getDistrict();
        excelParam.put("district", "直属".equals(district) ? "" : district);
        excelParam.put("phone", excelCreateParam.getBaseExcelParamDTO().getCustomerParamMap().get("phone"));
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Map> mapList = JSONUtil.toList(JSONUtil.toJsonStr(listMap), Map.class);
        listMap.clear();

        // 是否只生成模板
        Boolean onlyGenerateTemplateFlag = excelCreateParam.getExcelCreateConfig().getOnlyGenerateTemplateFlag();
        if (onlyGenerateTemplateFlag) {
            for (Map<String, Object> map : mapList) {
                for (String key : map.keySet()) {
                    map.put(key, "");
                }
                listMap.add(map);
                return;
            }
        }

        int num = 0;
        for (Map<String, Object> map : mapList) {

            // 判断是否能生成excel
            if (!isGenerateExcel(JSONUtil.toJsonStr(map))) continue;

            num ++;
            map.put("num", num);
            // 直系亲属是否有党员
            String key1 = ClassUtil.getField(DaYingLuRedRecordPdfDTO::getIsPartyMemberRecordKeeper);
            Boolean isPartyMemberRecordKeeper = (Boolean) map.get(key1);
            map.put(key1, isPartyMemberRecordKeeper ? "是" : "否");
            // 每桌餐标
            String key2 = ClassUtil.getField(DaYingLuRedRecordPdfDTO::getBanquetCost);
            Long banquetCost = Long.parseLong(map.get(key2).toString());
            map.put(key2, BigDecimal.valueOf(banquetCost).divide(new BigDecimal(100)));
            // 彩礼数（万元）
            String key3 = ClassUtil.getField(DaYingLuRedRecordPdfDTO::getDowryAmount);
            Long funeralCost = Long.parseLong(map.get(key3).toString());
            map.put(key3, BigDecimal.valueOf(funeralCost).divide(new BigDecimal(100*10000)));
            // 礼金
            String key4 = ClassUtil.getField(DaYingLuRedRecordPdfDTO::getGiftAmount);
            Long giftAmount = Long.parseLong(map.get(key4).toString());
            map.put(key4, BigDecimal.valueOf(giftAmount).divide(new BigDecimal(100)));
            // 备注是否主动正常备案
            String key5 = ClassUtil.getField(DaYingLuRedRecordPdfDTO::getIsRecorded);
            Boolean isRecorded = (Boolean) map.get(key5);
            map.put(key5, isRecorded ? "是" : "否");
            // 双方姓名
            String nameMarriagePerson = (String) map.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getNameMarriagePerson));
            String nameSpouse = (String) map.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getNameSpouse));
            List<String> names = new ArrayList<>();
            if (StringUtils.isNotBlank(nameMarriagePerson)) names.add(nameMarriagePerson);
            if (StringUtils.isNotBlank(nameSpouse)) names.add(nameSpouse);
            map.put("names", StrUtil.join("，", names));
            // 双方联系方式
            String phoneNumberMarriagePerson = (String) map.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getPhoneNumberMarriagePerson));
            String phoneNumberSpouse = (String) map.get(ClassUtil.getField(DaYingLuRedRecordPdfDTO::getPhoneNumberSpouse));
            List<String> phones = new ArrayList<>();
            if (StringUtils.isNotBlank(phoneNumberMarriagePerson)) phones.add(phoneNumberMarriagePerson);
            if (StringUtils.isNotBlank(phoneNumberSpouse)) phones.add(phoneNumberSpouse);
            map.put("phones", StrUtil.join("，", phones));

            listMap.add(map);
        }
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {
        rowDataMap.put("community", forSummaryBO.getSqfwFormSummaryNew().getCommunity());
    }

    /**
     * 如果在遍历之前就能够判断excel是否生成，那么就在这个方法中进行判断
     *
     * @param excelCreateParam excel生成所需的最为全面的数据
     * @param excelParam       excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        SqfwExcelJudgeRes sqfwExcelJudgeRes = new SqfwExcelJudgeRes();
        sqfwExcelJudgeRes.setFileIdentity(excelCreateParam.getExcelCreateConfig().getFileIdentity());

        List<SqfwExcelForSummaryBO> dataList = excelCreateParam.getDataList();
        if (dataList == null || dataList.size() == 0) {
            sqfwExcelJudgeRes.setCreateConditionFlag(false);
        }

        for (SqfwExcelForSummaryBO bo : dataList) {
            if (isGenerateExcel(bo.getSqfwFormSummaryNew().getParamData())) {
                sqfwExcelJudgeRes.setCreateConditionFlag(true);
                break;
            }
        }

        return sqfwExcelJudgeRes;
    }

    /**
     * 如果在遍历之后能够判断excel是否生成，那么就在这个方法中进行判断
     *
     * @param excelParam excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return DaYingLuRedRecordExcelImageConfigBO.class;
    }

    /**
     * 判断该条数据是否能生成excel
     * @param paramData
     * @return
     */
    private boolean isGenerateExcel(String paramData) {
        JSONObject jsonObject = JSON.parseObject(paramData);
        String key = ClassUtil.getField(DaYingLuRedRecordPdfDTO::getCelebratoryEvents);
        if (!RedWhite.RED.getName().equals(jsonObject.get(key))) return false;

        return true;
    }
}
